library(tidyverse)
## ── Attaching packages ───────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.2     ✓ purrr   0.3.4
## ✓ tibble  3.0.3     ✓ dplyr   1.0.2
## ✓ tidyr   1.1.2     ✓ stringr 1.4.0
## ✓ readr   1.3.1     ✓ forcats 0.5.0
## ── Conflicts ──────────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
time_series_confirmed_long <- read_csv(url("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv")) %>%
  rename(Province_State = "Province/State", Country_Region = "Country/Region")  %>% 
               pivot_longer(-c(Province_State, Country_Region, Lat, Long),
                             names_to = "Date", values_to = "Confirmed")
## Parsed with column specification:
## cols(
##   .default = col_double(),
##   `Province/State` = col_character(),
##   `Country/Region` = col_character()
## )
## See spec(...) for full column specifications.
time_series_deaths_long <- read_csv(url("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv")) %>%
  rename(Province_State = "Province/State", Country_Region = "Country/Region")  %>% 
  pivot_longer(-c(Province_State, Country_Region, Lat, Long),
               names_to = "Date", values_to = "Deaths")
## Parsed with column specification:
## cols(
##   .default = col_double(),
##   `Province/State` = col_character(),
##   `Country/Region` = col_character()
## )
## See spec(...) for full column specifications.
time_series_confirmed_long <- time_series_confirmed_long %>% 
  unite(Key, Province_State, Country_Region, Date, sep = ".", remove = FALSE)
time_series_deaths_long <- time_series_deaths_long %>% 
  unite(Key, Province_State, Country_Region, Date, sep = ".") %>% 
  select(Key, Deaths)
time_series_long_joined <- full_join(time_series_confirmed_long,
    time_series_deaths_long, by = c("Key")) %>% 
    select(-Key)
time_series_long_joined$Date <- mdy(time_series_long_joined$Date)
time_series_long_joined_counts <- time_series_long_joined %>% 
  pivot_longer(-c(Province_State, Country_Region, Lat, Long, Date),
               names_to = "Report_Type", values_to = "Counts")
pdf("images/time_series_example_plot.pdf", width=6, height=3)
time_series_long_joined %>% 
  group_by(Country_Region,Date) %>% 
  summarise_at(c("Confirmed", "Deaths"), sum) %>% 
  filter (Country_Region == "US") %>% 
    ggplot(aes(x = Date,  y = Deaths)) + 
    geom_point() +
    geom_line() +
    ggtitle("US COVID-19 Deaths")
dev.off()
## png 
##   2
ppi <- 300
png("images/time_series_example_plot.png", width=6*ppi, height=6*ppi, res=ppi)
time_series_long_joined %>% 
  group_by(Country_Region,Date) %>% 
  summarise_at(c("Confirmed", "Deaths"), sum) %>% 
  filter (Country_Region == "US") %>% 
    ggplot(aes(x = Date,  y = Deaths)) + 
    geom_point() +
    geom_line() +
    ggtitle("US COVID-19 Deaths")
dev.off()
## png 
##   2

US COVID-19 Deaths US COVID-19 Deaths

library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
ggplotly(
  time_series_long_joined %>% 
    group_by(Country_Region,Date) %>% 
    summarise_at(c("Confirmed", "Deaths"), sum) %>% 
    filter (Country_Region == "US") %>% 
    ggplot(aes(x = Date,  y = Deaths)) + 
      geom_point() +
      geom_line() +
      ggtitle("US COVID-19 Deaths")
 )
library(gganimate)
library(transformr)
theme_set(theme_bw())
data_time <- time_series_long_joined %>% 
    group_by(Country_Region,Date) %>% 
    summarise_at(c("Confirmed", "Deaths"), sum) %>% 
    filter (Country_Region %in% c("China","Korea, South","Japan","Italy","US"))
head(data_time)
## # A tibble: 6 x 4
## # Groups:   Country_Region [1]
##   Country_Region Date       Confirmed Deaths
##   <chr>          <date>         <dbl>  <dbl>
## 1 China          2020-01-22       548     17
## 2 China          2020-01-23       643     18
## 3 China          2020-01-24       920     26
## 4 China          2020-01-25      1406     42
## 5 China          2020-01-26      2075     56
## 6 China          2020-01-27      2877     82
p <- ggplot(data_time, aes(x = Date,  y = Confirmed, color = Country_Region)) + 
      geom_point() +
      geom_line() +
      ggtitle("Confirmed COVID-19 Cases") +
      geom_point(aes(group = seq_along(Date))) +
      transition_reveal(Date) 
animate(p,renderer = gifski_renderer(), end_pause = 15)

ppi <- 300
png("images/time_series_long_joined_deaths.png", width=3*ppi, height=3*ppi, res=ppi)
time_series_long_joined %>% 
  group_by(Date) %>% 
  summarise_at(c("Confirmed", "Deaths"), sum) %>% 
    ggplot(aes(x = Date,  y = Deaths)) + 
    geom_point() +
    geom_line() +
    ggtitle("Worldwide COVID-19 Deaths")
dev.off()
## png 
##   2

Worldwide COVID-19 Deaths

animated_Top10 <- time_series_long_joined %>% 
    group_by(Country_Region,Date) %>% 
    summarise_at(c("Confirmed", "Deaths"), sum) %>% 
    filter (Country_Region %in% c("Brazil","France","Italy", 
                                "United Kingdom", "US", "Mexico", "Spain", "India", "Iran", "Peru")) 

Top10 <- ggplot(animated_Top10, aes(x = Date,  y = Deaths, color = Country_Region)) + 
    geom_point() +
    geom_line() +
    ggtitle("Top 10 Countries with Highest Covid Deaths") +
    geom_point(aes(group = seq_along(Date))) +
      transition_reveal(Date) 
animate(Top10,renderer = gifski_renderer(), end_pause = 15)

time_series_confirmed_US <- read_csv(url("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_US.csv"))
## Parsed with column specification:
## cols(
##   .default = col_double(),
##   iso2 = col_character(),
##   iso3 = col_character(),
##   Admin2 = col_character(),
##   Province_State = col_character(),
##   Country_Region = col_character(),
##   Combined_Key = col_character()
## )
## See spec(...) for full column specifications.
time_series_confirmed_US_long <- time_series_confirmed_US %>% 
               pivot_longer(-c(Province_State, Country_Region, Lat, Long_, UID, iso2, iso3, code3, FIPS, Admin2, Combined_Key),
                            names_to = "Date", values_to = "Confirmed") 
library(lubridate)
time_series_confirmed_US_long$Date <- mdy(time_series_confirmed_US_long$Date)
head(time_series_confirmed_US_long)
## # A tibble: 6 x 13
##      UID iso2  iso3  code3  FIPS Admin2 Province_State Country_Region   Lat
##    <dbl> <chr> <chr> <dbl> <dbl> <chr>  <chr>          <chr>          <dbl>
## 1 8.40e7 US    USA     840  1001 Autau… Alabama        US              32.5
## 2 8.40e7 US    USA     840  1001 Autau… Alabama        US              32.5
## 3 8.40e7 US    USA     840  1001 Autau… Alabama        US              32.5
## 4 8.40e7 US    USA     840  1001 Autau… Alabama        US              32.5
## 5 8.40e7 US    USA     840  1001 Autau… Alabama        US              32.5
## 6 8.40e7 US    USA     840  1001 Autau… Alabama        US              32.5
## # … with 4 more variables: Long_ <dbl>, Combined_Key <chr>, Date <date>,
## #   Confirmed <dbl>
Confirmed_NC <- time_series_confirmed_US_long %>%
  group_by(Province_State, Date) %>%
  filter(Province_State == "North Carolina") %>%
  summarise(Confirmed_total = sum(Confirmed))
## `summarise()` regrouping output by 'Province_State' (override with `.groups` argument)
NC <- ggplot(Confirmed_NC, aes(x = Date,  y = Confirmed_total)) + 
    geom_point() +
    geom_line() +
  ggtitle("NC Confirmed COVID-19 Cases") +
  geom_point(aes(group = seq_along(Date))) +
      transition_reveal(Date)
animate(NC,renderer = gifski_renderer(), end_pause = 15)